package com.project2.controller;

import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketCoupon;
import com.project2.bean.po.MarketCouponUser;
import com.project2.bean.vo.AdminPageInfo;
import com.project2.service.AdminCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 优惠卷管理模块
 * @author Frank Zhu
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {

    @Autowired
    AdminCouponService couponService;

    @RequiresPermissions("admin:coupon:list")
    @RequestMapping("list")
    public ResultVo getCouponsByInfo(PageBo pageBo, String name, Short type, Short status){
        CommonData allCouponByInfo = couponService.getAllCouponByInfo(pageBo, name, type, status);
        return ResultVo.ok(allCouponByInfo);
    }

    /**
     * 优惠卷详情页下的持有过该优惠卷的用户信息
     * @param status 参数栏的优惠卷状态
     * @param userId 参数栏指定的用户id，如果输入，只显示该用户的信息
     * @param couponId 对应的优惠卷id
     * @return 返回持有过该优惠卷的用户信息
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:coupon:listuser")
    @RequestMapping("listuser")
    public ResultVo getCouponUsers(Short status, Integer userId, Integer couponId){
        CommonData<MarketCouponUser> couponUsers = couponService.getCouponUsers(status, userId, couponId);
        return ResultVo.ok(couponUsers);
    }

    /**
     * 获取详情页的对应优惠卷信息
     * @param id 优惠卷id
     * @return 优惠卷信息
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:coupon:read")
    @RequestMapping("read")
    public ResultVo getSpecifiedCoupon(Integer id){
        MarketCoupon specifiedCoupon = couponService.getSpecifiedCoupon(id);
        return ResultVo.ok(specifiedCoupon);
    }

    /**
     * 新建优惠卷
     * @param coupon 优惠卷信息
     * @return 返回一个带id的优惠卷，不然插入后需要刷新才会显示优惠卷的id，体验不好
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("create")
    public ResultVo createNewCoupon(@RequestBody MarketCoupon coupon){
        MarketCoupon insertedCoupon = couponService.createCoupon(coupon);
        return ResultVo.ok(insertedCoupon);
    }

    @RequiresPermissions("admin:coupon:update")
    @RequestMapping("update")
    public ResultVo updateCouponInfo(@RequestBody MarketCoupon updateInfo){
        MarketCoupon uptodateCoupon = couponService.updateCouponInfo(updateInfo);
        return ResultVo.ok(uptodateCoupon);
    }

    @RequiresPermissions("admin:coupon:delete")
    @RequestMapping("delete")
    public ResultVo deleteCoupon(@RequestBody MarketCoupon deleteInfo){
        couponService.deleteCoupon(deleteInfo.getId());
        return ResultVo.ok();
    }
}
